﻿using Sdeport.Transmit.Model;
using Sdeport.Transmit.Uitls;
using Sdeport.Transmit.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;

namespace SignTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if(message.Text.Length == 0)
            {
                MessageBox.Show(@"请选择文件！");
                return;
            }
            signMessage.Text = @"";
            DateTime StartTime = DateTime.Now;

            XmlDocument document = XmlHelper.LoadXml(message.Text);
            ResultInfo signature = SignatureHelper.ICSignXmlDocument(password.Text, document.InnerXml);

            DateTime EndTime = DateTime.Now;
            TimeSpan RunTimeSpan = EndTime - StartTime;
            RunTime.Content = "加签用时："+ RunTimeSpan.Seconds + "." + RunTimeSpan.Milliseconds + "秒";
            if (signature.Success)
            {
                signMessage.Text = (string)signature.Data;
            }
            else
            {
                MessageBox.Show((string)signature.FailReason);
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            ResultInfo keyNameResult = SignatureHelper.GetCertNo(password.Text);

            if (keyNameResult.Success)
            {
                MessageBox.Show(@"卡密码正确");
            }
            else
            {
                MessageBox.Show((string)keyNameResult.FailReason);
            }
        }


        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
 
            ResultInfo keyNameResult = SignatureHelper.GetCertNo(password.Text);

            if (keyNameResult.Success)
            {
                MessageBox.Show((string)keyNameResult.Data);
            }
            else
            {
                MessageBox.Show((string)keyNameResult.FailReason);
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog()
            {
                Filter = "xml文件(.xml)|*.xml"
            };
            var result = openFileDialog.ShowDialog();
            if (result == true)
            {
                message.Text = openFileDialog.FileName;
            }
            else
            {
                message.Text = @"";
            }
        }
    }
}
